<template>
  <div class="common-layout">
    <el-container>
      <!-- 头部导航栏 -->
      <el-header class="px-0 h-[80px]">
        <el-menu class="el-menu-demo"
                 mode="horizontal"
                 background-color="#38bdf8"
                 text-color="#000">
          <div></div>
          <h2 class="text-3xl font-bold h">DW商品后台管理系统</h2>
          <el-dropdown class="w-[200px] py-4">
            <el-button type="primary"
                       class="py-6">
              你好，{{nameX}}<el-icon class="el-icon--right">
                <ArrowDown />
              </el-icon>
            </el-button>
            <template #dropdown>
              <el-dropdown-menu class="w-[121px]">
                <el-dropdown-item @click="SignOut">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </el-menu>
      </el-header>
      <el-container>
        <!-- 左侧侧边栏 -->
        <el-aside :width="isCollapse ? '70px' : '250px'"
                  class="aside h-[849px]">
          <div class="coll bg-light-900"
               @click="DjSj">|||</div>
          <el-menu default-active="/index"
                   class="el-menu-vertical-demo"
                   :collapse="isCollapse"
                   :collapse-transition="false"
                   :unique-opened="true"
                   :router="true">
            <!-- 首页 -->
            <el-menu-item index="/index">
              <el-icon>
                <HomeFilled />
              </el-icon>
              <span>首页</span>
            </el-menu-item>
            <!-- 其他分类 -->
            <el-sub-menu v-for="item in Data"
                         :key="item.id"
                         :index="item.path + ''">
              <!-- 一级菜单 -->
              <template #title>
                <el-icon>
                  <HomeFilled />
                </el-icon>
                <span>{{item.name}}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item-group>
                <el-menu-item v-for="item in item.children"
                              :key="item.id"
                              :index="item.path + ''">
                  <el-icon>
                    <Grid />
                  </el-icon>
                  <span>{{item.name}}</span>
                </el-menu-item>
              </el-menu-item-group>
            </el-sub-menu>
          </el-menu>
        </el-aside>
        <!-- 主题内容 -->
        <el-main class="bg-light-700 main">
          <!-- 路由占位符 -->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import { ref } from 'vue'
import { ArrowDown, HomeFilled, UserFilled, GoodsFilled, List, Grid } from '@element-plus/icons-vue'
import router from '@/router'
import { useCookies } from '@vueuse/integrations/useCookies'
import { ElMessage, ElMessageBox } from 'element-plus'
import ListData from '@/hooks/ListData.js'
import Store from '@/store'

export default {
  name: 'Home',
  components: {
    ArrowDown,
    HomeFilled,
    UserFilled,
    GoodsFilled,
    List,
    Grid
  },
  setup () {
    const isCollapse = ref(false)
    const Data = ref(ListData)
    // 退出登录
    const SignOut = () => {
      ElMessageBox.confirm(
        '是否退出登录?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).then(() => {
        const cookie = useCookies()
        cookie.remove('token')
        router.push('/login')
        ElMessage({
          type: 'success',
          message: '退出登录成功!',
          duration: 800
        })
      }).catch(() => {
        ElMessage({
          type: 'info',
          message: '已取消退出登录!',
          duration: 800
        })
      })
    }
    // 点击收缩侧边栏
    const DjSj = () => {
      isCollapse.value = !isCollapse.value
    }
    // 获取登录传过来的名称
    const nameX = Store.state.user_name
    // 左侧侧边栏
    Data.value = ListData
    return {
      ArrowDown,
      HomeFilled,
      SignOut,
      isCollapse,
      DjSj,
      ListData,
      Data,
      UserFilled,
      GoodsFilled,
      List,
      Grid,
      nameX
    }
  }
}
</script>

<style lang="less" scoped>
.h {
  line-height: 80px;
  margin: 0 auto;
}
.coll {
  width: 100%;
  text-align: center;
  height: 40px;
  line-height: 40px;
  font-size: 20px;
  color: #000;
  letter-spacing: 0.2em;
  cursor: pointer;
}
.aside {
  transition: width 800ms;
  border-right: 1px solid #ccc;
}
.el-menu-vertical-demo {
  border: none;
}
</style>
